/**
 * $Id: A4CheatSheet.java 14 2011-02-01 08:06:29Z andriusms $
 */
package com.googlecode.kanjicards.format;

import com.googlecode.kanjicards.entity.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.*;
import java.util.*;

public class A4CheatSheet implements Renderable {

    private final BaseFont japaneseFont;
    private final BaseFont latinFont;

    public A4CheatSheet() throws RenderingException {
        japaneseFont = Utils.getJapaneseFont("fonts\\epkaisho.ttf");
        latinFont = Utils.getLatinFont("C:\\Windows\\fonts\\times.ttf");
    }

    public void render(Vocabulary vocabulary, String filename) throws RenderingException {
        try {
            Document document = new Document(PageSize.A4, 10, 10, 10, 10);
            PdfWriter.getInstance(document, Utils.fileOutputStream(filename));
            document.open();
            document.newPage();

            PdfPTable  table = new PdfPTable(3);
            table.setWidthPercentage(100);
            table.setExtendLastRow(true);

            int n = 0;
            Iterator it = vocabulary.getCardList().iterator();
            while(it.hasNext())
            {
                Card card = (Card) it.next();
                addCard(card, table);
                n++;
            }

            for(int i=0;i<3-(n%3);i++) table.addCell("");

            document.add(table);
            document.close();
        }
        catch(Exception e) {
            throw new RenderingException("Failed to render PDF file", e);
        }
    }

    private void addCard(Card card, PdfPTable mtable) throws DocumentException, IOException {

        PdfPCell cell = null;
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);
        table.setExtendLastRow(true);
        table.setWidths(new int[]{20, 40, 40});

        //kanji
        cell = new PdfPCell();
        cell.setPadding(10);
        cell.setPaddingTop(0);
        cell.setBorder(0);
        cell.addElement(new Paragraph(card.getKanji()+" ", new Font(japaneseFont, 30)));
        table.addCell(cell);

        //onyomi
        String onyomi = "";
        Iterator it = card.getOnyomiList().iterator();
        while(it.hasNext()) onyomi += it.next() + "\n";
        cell = new PdfPCell(new Paragraph(onyomi, new Font(japaneseFont, 10)));
        cell.setPadding(5);
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        table.addCell(cell);

        //kunyomi
        String kunyomi = "";
        it = card.getKunyomiList().iterator();
        while(it.hasNext()) kunyomi += it.next() + "\n";
        cell = new PdfPCell(new Paragraph(kunyomi, new Font(japaneseFont, 10)));
        cell.setPadding(5);
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        table.addCell(cell);

        //meaning
        cell = new PdfPCell(new Paragraph(card.getMeaning(), new Font(latinFont, 10)));
        cell.setPadding(5);
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setColspan(3);
        table.addCell(cell);

        //compounds
        Compound compound;
        cell = new PdfPCell();
        /*
        it = card.getCompoundList().iterator();
        while(it.hasNext())
        {
            compound = (Compound) it.next();
            cell = new PdfPCell(new Paragraph(compound.getJukugo(), new Font(ufnt, 15)));
            cell.setPadding(10);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_TOP);
            cell.setColspan(3);
        }
        */

        if(card.getCompoundList().size()!=0)
        {
        	compound = (Compound)card.getCompoundList().get(0);
        	cell = new PdfPCell();
        	cell.setPadding(10);
        	cell.setBorder(0);
        	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        	cell.setVerticalAlignment(Element.ALIGN_TOP);
        	cell.setColspan(3);

        	Paragraph p = new Paragraph();
        	p.setAlignment(Paragraph.ALIGN_CENTER);
        	p.add(new Chunk(compound.getPhrase(), new Font(japaneseFont, 15)));
        	p.add(new Chunk(""+compound.getReading()+"   ", new Font(japaneseFont, 10)));
        	cell.addElement(p);

        	//cell.addElement(new Paragraph(compound.getJukugo(), new Font(ufnt, 15)));
        	//cell.addElement(new Paragraph(compound.getYomi(), new Font(ufnt, 10)));
        }

        table.addCell(cell);

        mtable.getDefaultCell().setMinimumHeight(100);
        mtable.addCell(table);
    }
}
